package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.service.*;
import com.atguigu.stgy.model.dto.ApartmentSearchDTO;
import com.atguigu.stgy.model.dto.RoomDTO;
import com.atguigu.stgy.model.entity.*;
import com.atguigu.stgy.model.vo.RoomVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.admin.mapper.RoomInfoMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {
    @Resource
    RoomAttrValueService roomAttrValueService;
    @Resource
    RoomFacilityService roomFacilityService;
    @Resource
    RoomLabelService roomLabelService;
    @Resource
    RoomLeaseTermService roomLeaseTermService;
    @Resource
    RoomPaymentTypeService roomPaymentTypeService;
    @Resource
    GraphInfoService graphInfoService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateRoomDTO(RoomDTO roomDTO) {
        //1、保存或更新 room_info
        RoomInfo room = new RoomInfo();
        BeanUtils.copyProperties(roomDTO , room);
        //保存之前 判断新增还是更新
        boolean isUpdate = roomDTO.getId()!=null?true:false;
        this.saveOrUpdate(room);
        Long roomId = room.getId();
        //2、保存或更新 其他数据
        if(isUpdate){
            //删除中间表中的数据
            roomAttrValueService.remove(Wrappers.lambdaQuery(RoomAttrValue.class)
                    .eq(RoomAttrValue::getRoomId ,roomId));
            roomFacilityService.remove(Wrappers.lambdaQuery(RoomFacility.class)
                    .eq(RoomFacility::getRoomId ,roomId));
            roomLabelService.remove(Wrappers.lambdaQuery(RoomLabel.class)
                    .eq(RoomLabel::getRoomId ,roomId));
            roomLeaseTermService.remove(Wrappers.lambdaQuery(RoomLeaseTerm.class)
                    .eq(RoomLeaseTerm::getRoomId ,roomId));
            roomPaymentTypeService.remove(Wrappers.lambdaQuery(RoomPaymentType.class)
                    .eq(RoomPaymentType::getRoomId ,roomId));
            graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                    .eq(GraphInfo::getItemId ,roomId));
        }
        //保存或更新其他数据
        List<RoomAttrValue> roomAttrValues = roomDTO.getAttrValueIds().stream()
                .map(id -> {
                    RoomAttrValue roomAttrValue = new RoomAttrValue();
                    roomAttrValue.setAttrValueId(id);
                    roomAttrValue.setRoomId(roomId);
                    return roomAttrValue;
                }).collect(Collectors.toList());
        roomAttrValueService.saveBatch(roomAttrValues);

        List<RoomFacility> roomFacilities = roomDTO.getFacilityInfoIds().stream()
                .map(id -> {
                    RoomFacility roomFacility = new RoomFacility();
                    roomFacility.setFacilityId(id);
                    roomFacility.setRoomId(roomId);
                    return roomFacility;
                }).collect(Collectors.toList());
        roomFacilityService.saveBatch(roomFacilities);

        List<RoomLabel> roomLabels = roomDTO.getLabelInfoIds().stream()
                .map(id -> {
                    RoomLabel roomLabel = new RoomLabel();
                    roomLabel.setLabelId(id);
                    roomLabel.setRoomId(roomId);
                    return roomLabel;
                }).collect(Collectors.toList());
        roomLabelService.saveBatch(roomLabels);

        List<RoomLeaseTerm> roomLeaseTerms = roomDTO.getLeaseTermIds().stream()
                .map(id -> {
                    RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                    roomLeaseTerm.setLeaseTermId(id);
                    roomLeaseTerm.setRoomId(roomId);
                    return roomLeaseTerm;
                }).collect(Collectors.toList());
        roomLeaseTermService.saveBatch(roomLeaseTerms);


        List<RoomPaymentType> roomPaymentTypes = roomDTO.getPaymentTypeIds().stream()
                .map(id -> {
                    RoomPaymentType roomPaymentType = new RoomPaymentType();
                    roomPaymentType.setPaymentTypeId(id);
                    roomPaymentType.setRoomId(roomId);
                    return roomPaymentType;
                }).collect(Collectors.toList());
        roomPaymentTypeService.saveBatch(roomPaymentTypes);
        //保存/更新图片
        roomDTO.getGraphVoList().forEach(graphInfo -> {
            graphInfo.setId(null);
            graphInfo.setItemId(roomId);//关联房间和图片
            graphInfo.setItemType((byte) 2);
        });
        graphInfoService.saveBatch(roomDTO.getGraphVoList());
    }

    @Override
    public Page<RoomVO> pageItem(ApartmentSearchDTO searchDTO) {
        Page<RoomVO> page = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        List<RoomVO> roomVOS = baseMapper.selectRoomVOsByCondition(page,searchDTO);
        page.setRecords(roomVOS);
        return page;
    }
}
